<template>
    <el-dialog :title="!payload? '新增经销商' : '编辑经销商'" :show-close="false" v-model="addAgencyDialog" @close="handleRemoveModal(remove)" size="auto" :closeOnClickModal="false">
        <base-input @closedialog="handleClose">
            <el-row slot="body">
                <el-row class="body-top">
                    <el-form ref="form" :model="form" label-width="121px" class="add-task-form">
                        <el-form-item label="经销商编码">
                            <el-input v-model="form.code" placeholder="请输入经销商编码"></el-input>
                        </el-form-item>
                        <el-form-item label="单位名称">
                            <el-input v-model="form.company" placeholder="请输入单位名称"></el-input>
                        </el-form-item>
                        <el-form-item label="联系人">
                            <el-input v-model="form.contact" placeholder="请输入联系人"></el-input>
                        </el-form-item>
                        <el-form-item label="联系人座机">
                            <el-input v-model="form.phone1" placeholder="请输入联系人座机"></el-input>
                        </el-form-item>
                        <el-form-item label="联系人手机">
                            <el-input v-model="form.phone2" placeholder="请输入联系人手机"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱">
                            <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
                        </el-form-item>
                        <el-form-item label="地址">
                            <el-input v-model="form.address" placeholder="请输入地址"></el-input>
                        </el-form-item>
                        <el-form-item label="所在大区">
                            <el-select v-model="form.position_id" placeholder="请选择大区" @change="changePosition">
                                <el-option v-for="item in positionList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="省">
                            <el-select v-model="form.province_id" placeholder="请选择省" @change='changeProvinceHandler'>
                                <el-option v-for="item in provinceList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="市">
                            <el-select v-model="form.city_id" placeholder="请选择市">
                                <el-option v-for="item in getCityList()" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="经销商类别">
                            <el-select v-model="form.type" placeholder="请选择经销商类别">
                                <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="销售经理">
                            <el-select v-model="form.sale_user_id" placeholder="请选择销售经理" @change="changeSaler">
                                <el-option v-for="item in salerList" :key="item.id" :label="item.realname" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="起始日期">
                            <el-date-picker v-model="form.start_date" type="date" placeholder="请选择起始日期">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="结束日期">
                            <el-date-picker v-model="form.end_date" type="date" placeholder="请选择结束日期">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="保证金">
                            <el-input-number placeholder="请输入保证金" v-model="form.caution_money" :min='0'></el-input-number>
                        </el-form-item>
                        <!--<el-form-item label="预付款总额">
                                    <el-input-number  :disabled='!payload?false:true' placeholder="请输入预付款总额" v-model="form.count" :min='0'></el-input-number>
                        </el-form-item>
                         <el-form-item label="市场费用总额">
                                    <el-input-number :disabled='!payload?false:true' placeholder="请输入市场费用总额" v-model="form.count" :min='0'></el-input-number>
                        </el-form-item>-->
                        <el-form-item label="特殊说明">
                            <el-input v-model="form.more" :disabled='!payload?false:true' placeholder="请输入特殊说明"></el-input>
                        </el-form-item>
    
                        <el-form-item class="btns">
                            <el-button class="btn-orange" @click="handleSubmit">保存</el-button>
                        </el-form-item>
                    </el-form>
                </el-row>
            </el-row>
        </base-input>
    </el-dialog>
</template>
<!-- task_id = 19 -->
<script>
import BaseInput from '../../components/BaseInput'
import UploadPanel from '../../components/UploadPanel'
import Editor from '../../components/Editor'
import BaseList from '../../components/BaseList'
import { Loading } from 'element-ui'
import { Config } from '../../config/base'
import {
    RemoveModal
} from './mixins'
import {
    mapActions,
    mapState,
    mapGetters
} from 'vuex';
import {
    getDetail
} from '../../api/modules/dealer'
import { getCityList } from '../../components/Util'
import {
    MPop
} from '../../components/MessagePop'
export default {
    mixins: [RemoveModal, MPop],
    props: {
        remove: {
            type: String
        },
        payload: {}
    },
    data() {
        return {
            addAgencyDialog: true,
            form: {
                dealer_id: 0,
                company: '',
                contact: '',
                phone1: '',
                phone2: '',
                email: '',
                address: '',
                position_id: null,
                position_name:'',
                province_id: null,
                city_id: null,
                type: null,
                sale_user_id: null,
                sale_user_realname:'',
                start_date: '',
                end_date: '',
                caution_money: 0,
                more: '',
                _fn: null
            },
            isInit:false,
            loadingInstance: null
        }
    },
    mounted() {
        var vm = this;
        this.get_postion_list();
        this.get_city_list();
        this.get_saler_list();
        if (this.payload) {
            this.loadingInstance = Loading.service({
                text: '加载中，请稍后',
                fullscreen: true
            });
            setTimeout(() => {
                this.loadingInstance.close();
            }, Config.base_timeout);
            getDetail(this.payload).then(res => {
                if (res.data.res_code === 1) {
                    this.form = res.data.msg;
                    this.form.dealer_id = this.payload;
                    this.form._fn = function () {
                        vm.handleClose();
                        vm.showPop('保存成功！');
                    }
                    this.loadingInstance.close();
                    setTimeout(() => {
                            this.isInit = true;
                        }, 100);
                    }
            })
        }
        else
        {
            this.isInit = true;
        }
        this.form._fn = function () {
                        vm.handleClose();
                        vm.showPop('保存成功！');
                    }
    },
    computed: {
        ...mapState({
            typeList: state => state.agency.type_list,
            stateList: state => state.agency.state_list,
            positionList:state => state.city.position_list,
            provinceList:state => state.city.province_list,
            // cityList:state => state.city.city_list,
            salerList:state => state.saler.saler_list
        })
    },
    methods: {
        ...mapActions([
            'add_agency',
            'update_agency',
            'get_city_list',
            'get_postion_list',
            'get_saler_list'
        ]),
        handleClose() {
            this.addAgencyDialog = false;
        },
        handleSubmit() {
            if (this.payload) {
                this.update_agency(this.form);
            }
            else {
                this.add_agency(this.form);
            }
        },
        getCityList(){
            return getCityList(this.form.province_id,this.provinceList);
        },
        changeProvinceHandler() {
            if(this.isInit)
                this.form.city_id = null;
        },
        changePosition(v){
            for(var i=0;i<this.positionList.length;i++)
            {
                if(this.positionList[i].id === v)
                {
                    this.form.position_name = this.positionList[i].name;
                    break;
                }
            }
        },
        changeSaler(v){
            for(var i=0;i<this.salerList.length;i++)
            {
                if(this.salerList[i].id === v)
                {
                    this.form.sale_user_realname = this.salerList[i].realname;
                    break;
                }
            }
        }
    },
    components: {
        'base-input': BaseInput,
        'upload-panel': UploadPanel,
        'text-editor': Editor,
        'data-list': BaseList,
    },
}
</script>
<style lang="scss">
#add-agency-container {
    @import "base.scss";
    input,
    textarea {
        resize: none;
        outline: none;
    }
    .close-dialog-panel {
        position: absolute;
        top: -40px;
        right: 13.5px;
        z-index: 99999;
        font-size: 30px;
        cursor: pointer;
        &:before {
            // color: #fff;
            color: #757575;
        }
    }
    .el-dialog {
        width: 600px;
        background: none;

        .curriculum-list {
            .title {
                padding: 0 20px;
                h2 {
                    font-size: 16px;
                }
            }
            .search-bar {
                padding: 0 20px;
                margin-bottom: 20px;
                .el-button {
                    background: #FB843E;
                    border-radius: 4px;
                    border: 0;
                }
            }
        }

        .body-top {
            padding-bottom: 10px;
        }

        .el-dialog__header {
            background: #333333;
            border-radius: 4px 4px 0 0;
            padding: 16px;
        }
        .el-dialog__body {
            padding: 0;
            background: #fff;
            border-radius: 0 0 4px 4px;
        }
        .add-task-form {
            width: 80%;
            margin: 30px auto;
            .el-date-editor--datetimerange,
            .el-select {
                width: 100%;
            }
            input {
                border-radius: 0;
                border: 1px solid #CCCCCC;
            }
            .approval {
                .el-form-item__content {
                    text-align: left;
                    line-height: 38px;
                }
            }

            .row-container {
                height: 36px;
            }
            .el-date-editor {
                width: 100%;
            }
            .el-input-number {
                width: 100%;
            } // -------- 修改了单选框样式 ------------
            .el-radio__inner {
                display: inline-block;
                position: relative;
                border: 1px solid #bfcbd9;
                border-radius: 4px;
                box-sizing: border-box;
                width: 18px;
                height: 18px;
                background-color: #fff;
                z-index: 1;
                transition: border-color 0.25s cubic-bezier(.71, -.46, .29, 1.46), background-color 0.25s cubic-bezier(.71, -.46, .29, 1.46);
                border-color: #979797;
                background-color: transparent;
                &::after {
                    box-sizing: content-box;
                    content: "";
                    border: 2px solid #fff;
                    border-left: 0;
                    border-top: 0;
                    height: 8px;
                    left: 5px;
                    position: absolute;
                    top: 1px;
                    transform: rotate(45deg) scaleY(0);
                    width: 4px;
                    transition: transform 0.15s cubic-bezier(.71, -.46, .88, .6) 0.05s;
                    transform-origin: center;
                    border-radius: 0;
                    background-color: transparent;
                }
            }
            .is-checked {

                .el-radio__label {
                    border: 1px solid #5FA137;
                    color: #5FA137;
                }

                .el-radio__inner {
                    border-color: #5FA137;
                    background-color: #5FA137;
                }
                .el-checkbox__inner {
                    border-color: #5FA137;
                    background-color: #5FA137;
                }
                .el-radio__inner::after {
                    transform: rotate(45deg) scaleY(1);
                }
            } // -------- 修改了单选框样式 ------------
            @mixin el-upload-common($w) {
                .el-upload {
                    text-align: left;
                    width: 100%;
                    .el-icon-upload {
                        color: #999999;
                    }
                    .el-upload__tip {
                        font-size: 12px;
                        color: #757575;
                        letter-spacing: 0;
                        line-height: 20px;
                        text-align: left;
                        margin-top: 0;
                    }
                    .el-dragger {
                        // float: left;
                        // width: 240px;
                        border-radius: 0;
                        background-color: #F6F6F6;
                        border: 1px solid #CCCCCC;
                        width: 100%;
                        height: $w;
                        .el-icon-upload {
                            margin-left: 0; // margin-top: $_top;
                        }
                        .el-dragger__text {
                            font-size: 14px;
                            color: #757575;
                            letter-spacing: 0;
                            line-height: 14px;
                            margin-top: 20px;
                        }
                    }
                }
            }
            .upload-form1 {
                @include el-upload-common(200px);
            }
            .check-upload {
                text-align: left;
            }
            .inter-data {
                .el-form-item__label {
                    width: 100px !important;
                }
                .el-form-item__content {
                    margin-left: 102px !important;
                }
            }
            .upload-file-list {
                text-align: left;
                width: 40%;
                .datetime {
                    position: absolute;
                    right: -170px;
                    top: 16px;
                }
                .file-item {
                    cursor: pointer; // padding-top: 6px;
                    // padding-bottom: 6px;
                    // margin-top: 15px;
                    // margin-bottom: 15px;
                    position: relative;

                    &:hover {
                        color: #FB843E;
                    }
                    .filename {
                        width: 150px;
                        overflow: hidden;
                        height: 30px;
                        line-height: 30px;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        display: inline-block;
                    }
                    .el-icon-delete {
                        position: absolute;
                        right: -20px;
                        top: 10px;
                    }
                }
            }
            .btns {
                margin-top: 50px;
                .el-form-item__content {
                    margin-left: 0 !important;
                    margin-top: 10px !important;
                    line-height: 0;
                    .finish-btn {
                        margin-left: 0;
                        margin-top: 20px;
                        background: #FB843E;
                        border-radius: 4px;
                        width: 160px;
                        height: 36px;
                        border: 0;
                        &:last-child {
                            margin-left: 8px;
                        }
                    } // button {
                    //     width: 100px;
                    //     height: 36px;
                    //     background: #FFFFFF;
                    //     border: 1px solid #999999;
                    //     border-radius: 4px;
                    // }
                }

                .btn-pre {
                    float: left;
                    color: #333333;
                }

                .btn-orange {
                    background: #FB843E;
                    border: 1px solid #F06B1D;
                    border-radius: 4px;
                    color: #fff;
                    width: 200px;
                    height: 36px;
                }
            }
            .el-form-item__content {
                // margin-left: 0 !important;
                line-height: 0;
                .el-textarea {
                    .el-textarea__inner {
                        background: #FFFFFF;
                        border: 1px solid #CCCCCC; // height: 140px;
                        border-radius: 0; // width: 390px;
                    }
                }
                .editor {
                    .vueditor {
                        line-height: 100%;
                    }
                }
            }
        }
    }
}
</style>
